package com.xk.sport.backend.service;

import com.xk.sport.backend.dao.model.Admin;
import com.xk.sport.backend.dao.repository.AdminDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {
    @Autowired
    private AdminDao adminDao;

    public List<Admin> findAll() {
        return adminDao.findAll();
    }

    public Admin findOne(long id) {
        return adminDao.findOne(id);
    }

    public Admin add(Admin admin) {
        if (admin == null || admin.getId() > 0) {
            return null;
        }
        return adminDao.save(admin);
    }

    public Admin update(Admin admin) {
        if (admin == null || admin.getId() <= 0) {
            return null;
        }
        return adminDao.save(admin);
    }

    public boolean delete(long id) {
        adminDao.delete(id);
        return true;
    }

    public Admin findAdminByNameAndPassword(String name, String password) {
        return adminDao.findAdminByNameAndPassword(name, password);
    }

    @Cacheable(value = "admin")
    public Admin findOneWithCache(long id) {
        return adminDao.findOne(id);
    }
}
